
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EBAS_SP_GEM_STATUS]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[EBAS_SP_GEM_STATUS]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE [DBO].[EBAS_SP_GEM_STATUS] 
	@session varchar(255),
	@status varchar(255)
WITH ENCRYPTION
AS
	DECLARE 
		@rettighed int,
		@antal int,
		@guid uniqueidentifier

	SET NOCOUNT ON
	SET @guid = NULL

	CREATE TABLE #temp ( rett int )
	INSERT INTO #temp (rett)
	EXEC EBAS.DBO.EBAS_SP_SIKKERHED_VERIFICER_SESSION @session	

	SET @rettighed = (SELECT IsNull(max(rett),0) FROM #temp)
	
	IF 16 <= @rettighed
	BEGIN
		SET @guid = (SELECT GUID FROM EBAS_STATUS WHERE GUID = @guid)
		IF @guid IS NULL
		BEGIN
			SET @guid = newid()
			INSERT INTO EBAS_STATUS 
				SELECT @guid, @status
		END
		ELSE
			UPDATE EBAS_SYSTEM SET STATUS = @status
			WHERE GUID = @guid
		
		SELECT GUID, STATUS, 1 AS ANTAL	
		FROM EBAS_STATUS
		WHERE GUID = @guid
	END
	ELSE
		SELECT GUID, STATUS, 0 AS ANTAL FROM EBAS_STATUS WHERE GUID = NULL	
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
